using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;

namespace LenninSoftware.FormEditor
{    
    public class ToolBar : WebControl
    {


        private IEditForm _editForm;

        public IEditForm EditForm
        {
            get { return _editForm; }
        }

        public ToolBar(IEditForm editForm)
        {
            _editForm = editForm;            
        }

        protected Table CreateButton(string actionUrl, string title, string imageUrl )
        {

            Table table = new Table();
            table.CellPadding = 1;
            table.CellSpacing = 0;
            table.BorderWidth = 0;
            table.CssClass = "ms-toolbar";
            table.Width = new Unit(100, UnitType.Percentage);

            TableRow row = new TableRow();

            table.Rows.Add(row);


            TableCell cell = new TableCell();
            cell.CssClass = "ms-toolbar";
            cell.Attributes["nowrap"] = "true";

            row.Cells.Add(cell);

            HyperLink link = new HyperLink();
            link.NavigateUrl = actionUrl;
            link.CssClass = "ms-toolbar";
            link.Text = title;

            Image image = new Image();
            image.ImageAlign = ImageAlign.AbsMiddle;
            image.ImageUrl = imageUrl;
            image.BorderWidth = 0;
            image.Width = new Unit(16, UnitType.Pixel);
            image.Height = new Unit(16, UnitType.Pixel);

            link.Controls.Add(image);

            cell.Controls.Add(link);



            TableCell cell2 = new TableCell();
            cell2.CssClass = "ms-toolbar";
            cell2.Attributes["nowrap"] = "true";

            

            HyperLink link2 = new HyperLink();
            link2.NavigateUrl = actionUrl;
            link2.CssClass = "ms-toolbar";
            link2.Text = title;


            cell2.Controls.Add(link2);

            row.Cells.Add(cell2);


            return table;

        }

        protected void AddButton(HtmlTableRow row, Control button)
        {
            HtmlTableCell cell = new HtmlTableCell();
            cell.Attributes["class"] = "ms-toolbar";
            cell.Attributes["nowrap"] = "true";

            cell.Controls.Add(button);
            row.Cells.Add(cell);
        }

        protected void AddSeparator(HtmlTableRow row)        
        {
            HtmlTableCell cell = new HtmlTableCell();
            cell.Attributes["class"] = "ms-separator";
            cell.Controls.Add(new LiteralControl("|"));
            
            row.Cells.Add(cell);
        }

        protected void AddSpacer(HtmlTableRow row)
        {
            HtmlTableCell fillingSpace = new HtmlTableCell();
            fillingSpace.Attributes["width"] = "99%";
            fillingSpace.Attributes["class"] = "ms-toolbar";
            fillingSpace.Attributes["nowrap"] = "true";


            Image image = new Image();
            image.ImageUrl = "_layouts/images/blank.gif";
            image.Width = 1;
            image.Height = 18;

            fillingSpace.Controls.Add(image);

            row.Cells.Add(fillingSpace);

        }

        protected void AddRequiredMessage(HtmlTableRow row)
        {
            HtmlTableCell requiredMessage = new HtmlTableCell();
            requiredMessage.Attributes["nowrap"] = "true";

            row.Cells.Add(requiredMessage);

            requiredMessage.Controls.Add(new LiteralControl("<span ID=\"reqdFldTxt\" style=\"white-space: nowrap;padding-right: 3px;\" class=\"ms-descriptiontext\"><span class=\"ms-formvalidation\">*</span> indicates a required field</span>"));      
        }


        protected virtual void CreateButtons( HtmlTableRow row )
        {
             //http://s-ws32-sharep/BA/Lists/Documents/Forms/EditForm.aspx?ID=40
            string editLink = "#";

            if (EditForm != null && EditForm.Item != null)
            {
                editLink = SPContext.Current.Web.Url + "/Lists/" + EditForm.List.RootFolder.Name + "/Forms/EditForm.aspx?ID=" +
                   EditForm.Item.ID;
            }

            string spellingLink = "javascript:SpellCheckEntirePage('http:\\u002f\\u002fs-ws32-sharep\\u002fBA/_vti_bin/SpellCheck.asmx', 'http:\\u002f\\u002fs-ws32-sharep\\u002fBA/_layouts/SpellChecker.aspx');";


            switch (EditForm.ControlMode)
            {
                case SPControlMode.Display:

                    AddButton(row, CreateButton(editLink, "Edit Item", "/_layouts/images/edititem.gif"));
                    AddSeparator(row);
                    AddButton(row, CreateButton("#", "Delete Item", "/_layouts/images/delitem.gif"));
                    //AddSeparator(row);
                    AddSpacer(row);
                    break;

                case SPControlMode.New:

                    AddButton(row, CreateButton(spellingLink, "Spelling ...", "/_layouts/1033/images/rte2spchk.gif"));
                    AddSpacer(row);
                    AddRequiredMessage(row);

                    break;
            }
        }

        protected override void CreateChildControls()
        {

            HtmlTable table = new HtmlTable();
            table.CellPadding = 2;
            table.CellSpacing = 0;
            table.Border = 0;
            table.Attributes["class"] = "ms-toolbar";
            table.Width = "100%";

            
            HtmlTableRow row = new HtmlTableRow();

            table.Rows.Add(row);

            CreateButtons(row);
            Controls.Add(table);

        } 
    }

}
